<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加</title>
    <link rel="stylesheet" href="/static/style.css">
    <link rel="stylesheet" href="/static/layui/css/layui.css">
</head>
<body>
<div class="vote-add">
    <form class="layui-form layui-form-pane" lay-filter="addForm" action="">
        <div class="layui-form-item" pane>
            <label class="layui-form-label">事件名称</label>
            <div class="layui-input-block">
                <input type="text" name="voteName" lay-verType="tips" required lay-verify="required|voteName" placeholder="请输入投票事件名称" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline" pane>
            <label class="layui-form-label">用户总票数</label>
            <div class="layui-input-block">
                <input type="text" name="voteLimit" lay-verType="tips" required lay-verify="required|number" placeholder="用户可投总票数" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline" pane>
            <label class="layui-form-label">同一人可投</label>
            <div class="layui-input-block">
                <input type="text" name="voteForOne" lay-verType="tips" required lay-verify="required|number" placeholder="同一候选人最多可投" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">候选人投票</label>
            <div class="layui-input-block">
                <input type="checkbox" id="vote_switch" name="isSelfVoted" value="false" lay-filter="selfVoted" lay-verType="tips" lay-skin="switch" lay-text="允许|禁止">
            </div>
        </div>
        <div class="layui-form-item layui-inline" pane>
            <label class="layui-form-label">投票开始日期</label>
            <div class="layui-input-block">
                <input type="text" name="startTime" required lay-verType="tips" lay-verify="required" class="layui-input" id="start_time">
            </div>
        </div>
        <div class="layui-form-item layui-inline" pane>
            <label class="layui-form-label">投票截止日期</label>
            <div class="layui-input-block">
                <input type="text" name="endTime" required lay-verType="tips" lay-verify="required" class="layui-input" id="end_time">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">简介</label>
            <div class="layui-input-block">
                <textarea name="details" required lay-verify="required|details" placeholder="请输入简介，不多于200字" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center">
                <button class="layui-btn" type="button" lay-submit lay-filter="addFormBtn">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/static/layui/layui.js"></script>
<script>
    layui.extend({
        voteList: '/static/lib/voteList'
    });
    layui.use(['form', 'jquery', 'laydate', 'layer'], function () {
        var $ = layui.$
            ,form = layui.form
            ,layer = layui.layer
            ,laydate = layui.laydate;

        laydate.render({
            elem: '#start_time'
            ,type: 'datetime'
            ,value: new Date()
        });

        laydate.render({
            elem: '#end_time'
            ,type: 'datetime'
            ,value: new Date()
        });

        //表单验证
        form.verify({
            voteName: function (v, i) {
                if (v.length > 15){
                    return '事件名称要小于15字'
                }
                if (/^\d+\d+\d$/.test(v)){
                    return '事件名称不能全为数字'
                }
            }
            ,details: function (v, i) {
                if (v.length > 300){
                    return '简介要小于300字'
                }
            }
        });

        //为表单设置初始值
        form.val("addForm", {
            "voteLimit": "5"
            ,"voteForOne": "1"
            ,"isSelfVoted": false
        });


        //监听提交
        form.on('submit(addFormBtn)', function (data) {

            var startTime = $('#start_time').val();
            var endTime = $('#end_time').val();

            if (startTime >= endTime) {
                layer.alert('结束时间不能早于开始时间', {icon: 2});
                return false;
            }

            var field = data.field;
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

            field['isSelfVoted'] = $('#vote_switch').val();
            console.log(field);
            $.ajax({
                url: '/vote'
                ,type: 'POST'
                ,contentType: 'application/json'
                ,data: JSON.stringify(field)
                ,success: function (data) {
                    if (data.status == 200){
                        parent.layui.table.reload('voteList',{page: {curr: 1}}); //重载表格
                        layer.confirm(data.msg, {
                            btn: ['确定']
                        }, function () {
                            parent.layer.close(index);
                        })
                    } else {
                        layer.confirm(data.msg, {
                            btn: ['确定']
                        });
                    }

                }
            });
            return false;
        });
    });
</script>
</body>
</html>